<template>
    <div class="app-container">
        <levelbar></levelbar>
        <TableLayout form-title="">
            <div slot="form-title" class="table-title"></div>
            <el-form :model="query" ref="query" slot="form" label-width="80px" label-position="right">
                <el-row :gutter="41">
                    <el-col :span="8">
                        <el-form-item label="视频名称" prop="videoName">
                            <el-input v-model="query.videoName" placeholder="请输入视频名称" style="width:100%" :maxlength="14"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="付费形式" prop="paymentForm">
                            <el-select v-model="query.paymentForm" clearable placeholder="请选择" style="width:100%;">
                                <el-option v-for="item in paymentFormList" :key='item.value' :label='item.label' :value='item.value'></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="所属分类" prop="classify">
                            <el-select v-model="query.classify" clearable placeholder="请选择" style="width:100%;">
                                <el-option v-for="item in classifyList" :key='item.value' :label='item.label' :value='item.value'></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="41">
                    <el-col :span="8">
                        <el-form-item label="创建日期" prop="creatTime">
                            <el-date-picker v-model="query.creatTime" type="date" placeholder="请选择" :editable='false' style="width:100%;"></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="状态" prop="state">
                            <el-select v-model="query.state" clearable placeholder="请选择" style="width:100%;">
                                <el-option v-for="item in stateList" :key='item.value' :label='item.label' :value='item.value'></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <div class="top-line">
                    <el-button type="primary" class="top-btn" @click="onSubmit">查询</el-button>
                </div>
            </el-form>
            <div class="table-title" slot="table-title">
                <div class="btnPosition">
                    <el-button @click="batchDelete()">批量删除</el-button>
                    <el-button @click="addVideo()">添加视频</el-button>
                </div>
            </div>
            <el-table :data="tableData" border v-loading="loading" style="width: 100%" @selection-change="handleSelectionChange">
                <el-table-column type="selection" align="center" width="55" :selectable="fn"></el-table-column>
    
                <el-table-column label="视频名称" align="center">
                    <template scope="scope">
                        <span class="link-src" @click="readVideo(scope.row)">{{scope.row.sVideoName | isNull}}</span>
                    </template>
                </el-table-column>
    
                <el-table-column label="付费形式" align="center">
                    <template scope="scope">
                        <span>{{scope.row.sPayType==0?'免费':scope.row.sPayType==1?'优惠':'--'}}</span>
                    </template>
                </el-table-column>
    
                <el-table-column label="所属分类" align="center">
                    <template scope="scope">
                        <span>{{scope.row.sType==0?'课内':scope.row.sType==1?'课外':'--'}}</span>
                    </template>
                </el-table-column>
    
                <el-table-column label="创建人" align="center">
                    <template scope="scope">
                        <span>{{scope.row.createName | isNull}}</span>
                    </template>
                </el-table-column>
    
                <el-table-column label="创建时间" width="170" align="center">
                    <template scope="scope">
                         <span>{{ scope.row.sCreateTime?scope.row.sCreateTime.substr(0, 16):'--' }}</span>
                    </template>
                </el-table-column>
    
                <el-table-column label="浏览量" align="center">
                    <template scope="scope">
                        <span>{{ scope.row.sVisitNum | isNull}}</span>
                    </template>
                </el-table-column>
    
                <el-table-column label="状态" align="center">
                    <template scope="scope">
                        <span>{{ scope.row.sStatus==0?'下架':scope.row.sStatus==1?'上架':'--' }}</span>
                    </template>
                </el-table-column>
    
                <el-table-column label="评论" align="center">
                    <template scope="scope">
                        <span>{{ scope.row.commentNum | isNull}}</span>
                    </template>
                </el-table-column>

                <el-table-column label="权重" align="center">
                    <template scope="scope">
                        <span>{{ scope.row.sPriority | isNull }}</span>
                    </template>
                </el-table-column>

                <el-table-column label="操作" width="200px">
                    <template scope="scope">
                        <el-button v-if="scope.row.sStatus==0" type="text" class="btn-success" @click="unShelve(scope.row)">上架</el-button>
                        <el-button v-if="scope.row.sStatus==1" type="text" class="btn-success" @click="unShelve(scope.row)">下架</el-button>
                        <el-button v-if="scope.row.sStatus==0" type="text" class="btn-success" @click="edit(scope.row)">编辑</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination layout="total, sizes, prev, pager, next, jumper" :total="pageQuery.total" :current-page.sync="pageQuery.currentpage" :page-sizes="config.pageSizes" :page-size="config.pageSize" @size-change="handleSizeChange" @current-change="handleCurrentChange">
                </el-pagination>
            </div>
        </TableLayout>
    </div>
</template>

<script>
    import moment from 'moment';
    import Levelbar from '../../layout/Levelbar';
    import TableLayout from 'components/TableLayout';
    import { pageGuidance, listGuidanceType, updateGuidanceStatus, deleteBatchGuidance } from 'api/onlineTutor';
    import { mapState, mapActions } from 'vuex';

    export default {
        name: 'subjectTutor',
        components: { Levelbar, TableLayout },
        data() {
            return { 
                loading: false,  
                paymentFormList: [{
                    value: '',
                    label: '请选择'  
                }, {
                    value: '0',
                    label: '免费'
                }, {
                    value: '1',
                    label: '优惠'
                }],
                classifyList: [{
                    value: '',
                    label: '请选择'  
                }, {
                    value: '0',
                    label: '课内'
                }, {
                    value: '1',
                    label: '课外'
                }],
                stateList: [{
                    value: '',
                    label: '请选择'
                }, {
                    value: '0',
                    label: '下架'
                }, {
                    value: '1',
                    label: '上架'
                }],
                tableData: [],
                pageQuery: {
                    currentpage: 1,
                    total: 0
                },
                query: {
                    videoName: '',
                    paymentForm: '',
                    classify: '',
                    creatTime: '',
                    state: ''
                },
                multipleSelection: []
            }
        },
        created() {
            // this.listGuidanceTypeFu();
            this.queryData();
        },
        watch: {// 局部刷新列表
            cacheUpdated(val) {
                val && this.queryData();
            }
        },
        computed: {
            ...mapState({
                config: state => state.config.table,
                cacheUpdated: state => state.app.cacheUpdated // 局部刷新列表
            })
        },
        methods: {
            ...mapActions(['ChangePageSize']),
            handleSizeChange(val) {
                this.pageQuery.currentpage = 1;
                this.ChangePageSize(val);
                this.queryData();
            },
            handleCurrentChange(val) {
                this.pageQuery.currentpage = val;
                this.queryData();
            },
            onSubmit() {
                this.pageQuery.currentpage = 1;
                this.queryData();
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            fn(row) {
                if (row.sStatus!=1) {
                    return true;
                } else {
                    return false;
                }
            },

            // 所属分类
            listGuidanceTypeFu() {
                listGuidanceType().then(response => {
                    if (response.data.status == 200) {
                        this.classifyList = response.data.content;  
                    } else {
                        this.$message.error(response.data.errorMsg);
                    }
                    
                }).catch((e) => {
                    this.loading = false;    
                });
            },
            queryData() {

                let sVideoName = this.query.videoName; // 视频名称 
                let sType = this.query.classify; // 所属分类  
                let sPayType = this.query.paymentForm; // 付费形式 
                let time = this.query.creatTime==''?'':moment(this.query.creatTime).format('YYYY-MM-DD'); // 创建时间 
                let sStatus = this.query.state; // 状态
                pageGuidance(this.pageQuery.currentpage, this.config.pageSize, sVideoName, sType, sPayType, time, sStatus).then(response => {
                    this.loading = false;
                    this.tableData = [];
                    if (response.data.status == 200) {
                        this.tableData = response.data.content.list;
                        this.pageQuery.total = response.data.content.pagination.total;
                    } else {
                        this.$message.error(response.data.errorMsg);
                    }
                    
                }).catch((e) => {
                    this.loading = false;    
                });
            },

            // 删除功能
            deleteVideo(videoList) {
                this.$confirm('确定删除选中视频？', '', {
                    closeOnClickModal: false,
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: ' ', 
                    title: '  '
                }).then(() => {
                    let pIds = videoList;
                    deleteBatchGuidance(pIds).then(response => {
                        if (response.data.status == 200) {
                            this.$message.success(response.data.errorMsg);
                            this.queryData();
                        } else {
                            this.$message.error(response.data.errorMsg);
                        }
                    })
                }).catch((e) => {
                    this.$message.info('已取消删除');     
                });
                
            },
            
            // 批量删除
            batchDelete() {
               
                let videoList = [];

                if (this.multipleSelection.length == 0) {
                    this.$message('请选择需要删除的视频');
                    return;
                }
                for (let index=0; index<this.multipleSelection.length;index++) {
                    videoList.push(this.multipleSelection[index].pId);
                }

                this.deleteVideo(videoList); 
                
            },

            // 添加视频
            addVideo() {
                this.$router.push({ path: 'addVideo' });
            },

            // 编辑
            edit(obj) {
                this.$router.push({path: `editVideo/${obj.pId}`});
            },

            // 下架/上架
            unShelve(obj) {
    
                let title = obj.sStatus==0?'上架':'下架'
                this.$confirm('是否确认'+title+'？', '', {
                    closeOnClickModal: false,
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: ' ', 
                    title: '  '
                }).then(() => {
                    updateGuidanceStatus(obj.pId).then(response => {
                        if (response.data.status == 200) {
                            this.$message.success(response.data.errorMsg);
                            this.queryData();
                        } else {
                            this.$message.error(response.data.errorMsg);
                        }
                    })
                }).catch((e) => {
                    this.$message.info('已取消');     
                });
            },

            // 查看详情
            readVideo(obj) {
                this.$router.push({path: `videoDetail/${obj.pId}`});
            }
        }

    }
</script>

<style lang="scss" scoped>
    .app-container {
        position: relative;
        padding: 20px 20px 10px;
        $bgcolor: #fff;
        .app-levelbar {
            height: 35px;
        }
    }
</style>

